package com.example.graduatetest.model;


import android.util.Log;

import com.example.graduatetest.bean.Student;
import com.example.graduatetest.callback.LoginCallBack;
import com.example.graduatetest.utils.Constant;
import com.google.gson.Gson;
import org.json.JSONObject;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginModel {
    private String www = Constant.www;
    private String studentUrl = "/student/login";
    private String teacherUrl = "/teacher/login";
    private String url;

    //登录的网络请求
    public void login(final String number, final String password,final boolean isStudent, final LoginCallBack callBack){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    OkHttpClient client = new OkHttpClient();//新建一个OKHttp的对象
                    RequestBody body = new FormBody.Builder()
                            .add("number", number)
                            .add("password", password)
                            .build();
                    Log.e("haha", password);
                    if(isStudent){
                        url = studentUrl;
                    }else{
                        url = teacherUrl;
                    }
                    Request request = new Request.Builder()
                            .url(www+url)
                            .post(body)
                            .build();//创建一个Request对象

                    Response response = client.newCall(request).execute();//发送请求获取返回数据
                    String responseData = response.body().string();//处理返回的数据
                    Log.e("haha", responseData);
                    callBack.onSuccess(responseData);

                }catch (Exception e){
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();

    }



}
